<%@page import="java.rmi.RemoteException"%>
<%@page import="java.sql.SQLException"%>
<%@page import="org.apache.jsp.ForumHtml.notifier_jsp"%>
<%@page import="clientData.*"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Vector"%>
<%@page import="java.rmi.Naming"%>
<%@page import="exception.*"%>
<%@page import="server.ForumHandlerInt"%>
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">





<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posts</title>


<!-- this script is notifing the user about his friends -->

<script type="text/javascript" src="jquery.js"></script>


<%@ include file="scripts.jsp" %>

  
  
 
<link href="styles.css" rel="stylesheet" type="text/css">
    <link href="tableDesign.css" rel="stylesheet" type="text/css">
    <script language="JavaScript" type="text/JavaScript">
		<!--
		function MM_preloadImages() { //v3.0
		  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
			var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
		}
		-->
	</script>
</head>

<%@ include file="serverConnection.jsp" %>
<%

	int topicId = Integer.parseInt(request.getParameter("topicId"));
	
	session.setAttribute("topicId",topicId);
	String _topicName = request.getParameter("topicName");
	
	
    if(request.getParameter("forumId")!=null) 
    	session.setAttribute("forumId",Integer.parseInt(request.getParameter("forumId")));

	if (session.getAttribute("userName")==null || session.getAttribute("forumId")==null)
	{
		response.sendRedirect("loggin.jsp");
		response.flushBuffer();
		return;
	}



%>


<body link="#6C8EBD">
<div id="main"> 
	<div id="header">
		<div id="logo"><img src="Images/logo1.jpg" alt="logo" width="490" height="182"></div>
		<div id="picture"><img src="Images/logo2.jpg" alt="pic" width="289" height="156"></div>
	</div>
	<div id="line"><img src="Images/line.jpg" alt="line" width="779" height="4"></div>
    <div id="navigation">
		POSTS OF TOPIC:<%=_topicName%>
	</div>
	<div id="line2"><img src="Images/line.jpg" alt="line" width="779" height="4"></div>
	<div id="text" ice:editable="*">
		<div id="bar">
			<div class="bluetxt" id="linktxt">
       <p><a class="bluetxt" href="friends.jsp">manage friends</a></p>
      <p><a class="bluetxt" href="mainScreen.jsp"> main screen</a>  </p>
      <p><a class="bluetxt" href="topics.jsp?forumId=<%=(Integer)session.getAttribute("forumId")%>&forumName=<%=(String)session.getAttribute("forumName")%>"> forum: <%=(String)session.getAttribute("forumName")%></a>  </p>
  	  <p><a class="bluetxt" href="addPost.jsp?topicId=<%=topicId%>&topicName=<%=_topicName%>"> add Post</a>  </p>
        <p><a class="bluetxt" href="index.jsp"  target="_top" onClick= "logoutFunc();">logout</a>  </p>           
			</div>
		</div>
		<div id="text2">
			<div class="contentTXT" id="text2txt">

    
<table border=2 align="center">
<%

LinkedList<PostDataInt> posts = new LinkedList<PostDataInt>();

try
{
	 posts = forum.getPostList((String)session.getAttribute("userName"),(String)session.getAttribute("password"),topicId,(Integer)session.getAttribute("forumId"));
}
catch(NotLoggedInException t)
{

	response.sendRedirect("loggin.jsp");
	response.flushBuffer();
	return;
	
}
catch(SQLException s){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,
		"SQL server is down");
	
		response.flushBuffer();
		return;
	   
	   
}
catch(RemoteException u){
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,
		"forum server is down");
	
		response.flushBuffer();
		return;
}

%>

<thead>

<tr>
<th>TITLE</th>
<th>CONTENT</th>
<th>CREATOR</th>
<th>CREATED</th>
<th>MODIFIED</th>
<th></th>
</tr>
</thead>

<tbody>

<%
	
    for ( PostDataInt pd:posts ) {
        %>
        
        <tr id=<%=pd.getId()%>>
        	
        	<td id=title<%=pd.getId()%>><%= pd.getTitle()%></td>
        
        	
	
        	<td id=content<%=pd.getId()%>> <textarea readonly="readonly"><%=pd.getContent()%></textarea></td>
			
			<td id=creator<%=pd.getId()%>><%= pd.getCreator().getUserName()%></td>
        
        	<td id=creation<%=pd.getId()%>><%= pd.getCreationTime().toLocaleString()%></td>
        	
        	<td id=editing<%=pd.getId()%>><%= pd.getEditingTime().toLocaleString()%></td>
        	<%
				if ((((String)session.getAttribute("permission")).equals("ADMIN"))||(pd.getCreator().getUserName().equals(((String)session.getAttribute("userName"))))){
			%>
        	<td id=edit<%=pd.getId()%>><input type=button onClick="procLink('<%=pd.getId()%>');" value='edit post'><br/>
        	<input type=button onClick="removePost('<%=pd.getId()%>');" value='delete'></input></td>
        	<%
				}
        	%>
		</tr>
        
        
        
       
        <%
    }
%>
</tbody>
</table>


<form name="foo" method=post action="editPost.jsp">
	
	<input type="hidden" name="postId" value="">
	<input type="hidden" name="postTitle" value="">
	<input type="hidden" name="postContent" value="">
	<input type="hidden" name="topicId" value="">
	<input type="hidden" name="topicName" value="">

</form>


<script language="Javascript">
  function procLink(pId) {
	  
	 
    document.foo.postId.value = pId
    
    document.foo.postTitle.value = $("#title"+pId).html()
    
    document.foo.postContent.value = $("#content"+pId+" textarea").html()
    
    document.foo.topicId.value = <%=request.getParameter("topicId")%>
    
    document.foo.topicName.value = "<%=request.getParameter("topicName")%>"
    
    document.foo.submit()
}
  
  
function removePost(pId)
{
	$.ajax({
		type : "GET",
		url : "deletePost.jsp?postId="+pId
	});	
	
	$("#"+pId).remove();
}
  </script>

			</div>
		</div>
	</div>
<div id="Num"><img src="Images/line.jpg" alt="line" width="779" height="4"></div>
<div id="footer">
<div class="footerTXT" id="footertxt"><a class="footerTXT">Designed by ariel nir yuri gilad itzhaki and marina</a> </div>   </div>
</div>
</div>

</body>   
</html>